package com.ziyun.erp.web.modules.goods.controller;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class GoodsAspect {

    @Pointcut("@annotation(com.ziyun.erp.modules.goodsMonitor.annotation.OptionGoodsMonitoring)")
    public void optionMonitor() {

    }

    @Around("optionMonitor()")
    public Object goodsInterceptor(ProceedingJoinPoint pjp) {
        //获取签名
        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        //判断签名是否为方法签名
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        //将签名强转成方法类
        msig = (MethodSignature) sig;
        Object target = pjp.getTarget();
        //实际参数值
        Object[] parameterValue = pjp.getArgs();
        try {
            //根据方法名和形参确定方法
            Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
            Class<?>[] parameterTypes = currentMethod.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println(parameterType.getName());
            }
            System.out.println(currentMethod);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Object proceed = null;
        try {
            proceed = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }


}
